package com.atguigu.demo.customer.controller;


import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {

    @Autowired    // 何时装配： 1 启动时装配 (默认) 2 用的时候装配
    @Qualifier("wang")
    CustomerService customerService;


    @GetMapping("/customer")     // 键值对参数一般用于多个条件 辅助条件
    public String customer(@RequestParam("name") String name ,@RequestParam(value = "age",required = false,defaultValue = "0") Integer age){
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name", name).eq("age", age));

        return "customerList:"+customerList;
    }
    @GetMapping("/customer/{id}.html")    //路径参数 一般是唯一性标识
    public String customer(@PathVariable("id") String customerId){
    //    Customer customer = customerService.getCustomerById(customerId);
        Customer customer = customerService.getById(customerId);
        return "customer   :"+customer;
    }

    @PostMapping("/customer")
    public  String saveCustomer(@RequestBody Customer customer){
        customerService.saveCustomer(customer);
        // customerService.saveOrUpdate(customer);
        return customer.toString();
    }

}
